package com.syc.test;

import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.util.CollectionUtils;

/**
 * @author hhj
 * @description
 * @date 2021-03-18 10:10
 */
public class HtmlUtils {

    private HtmlUtils(){}


    public static String getText(String input,Integer maxLength){
        if (input == null || "".equals(input.trim())) {
            return "";
        }
        Document document = Jsoup.parse(input);
        if(document!=null){
            String text = document.text();
            if(StringUtils.isNotEmpty(text)){
                text=text.replaceAll("\r|\n", "");
                if(maxLength!=null && maxLength<text.length()){
                    return text.substring(0,maxLength);
                }else {
                    return text;
                }
            }
        }
        return "";
    }

    public static String splitAndFilterStringNoSuffix(String input, int length) {
        if (input == null || "".equals(input.trim())) {
            return "";
        }
        Document document = Jsoup.parse(input);
        if(document!=null){
            String text = document.text();
            if(StringUtils.isNotEmpty(text)){
                if(text.length()>length){
                    return text.substring(0,length).replaceAll("\r|\n", "");
                }else {
                    return text.replaceAll("\r|\n", "");
                }
            }
        }
        return "";
    }

    public static String splitAndOnelineStringNoSuffix(String input, int length) {
        if (input == null || "".equals(input.trim())) {
            return "";
        }
        Document document = Jsoup.parse(input);
        if(document!=null){
            String text = document.text();
            Elements pTags = document.getElementsByTag("p");
            if(!CollectionUtils.isEmpty(pTags)){
                for (Element pTag : pTags) {
                    if(StringUtils.isNotEmpty(pTag.text().trim())){
                        String pText=pTag.text().trim().replaceAll("(\\/|\\.|\\&|\\%|\\\\|\\?|\\s+)","");
                        if(StringUtils.isNotEmpty(pText)){
                            text=pText;
                            break;
                        }
                    }
                }
            }
            if(StringUtils.isNotEmpty(text)){
                if(text.length()>length){
                    return text.substring(0,length).replaceAll("\r|\n", "");
                }else {
                    return text.replaceAll("\r|\n", "");
                }
            }
        }
        return "";
    }

    public static String splitAndFilterString(String input, int length) {
        if (input == null || "".equals(input.trim())) {
            return "";
        }
        Document document = Jsoup.parse(input);
        if(document!=null){
            String text = document.text();
            if(StringUtils.isNotEmpty(text)){
                if(text.length()>length){
                    return text.substring(0,length).concat("......").replaceAll("\r|\n", "");
                }else {
                    return text.concat("......").replaceAll("\r|\n", "");
                }
            }
        }
        return "";
    }

}
